// 使用 combineReducers 来把多个 reducer 创建成一个根 reducer。
// init action 后会挂在到对应的命名空间下
// counterReducer => state.counterReducer
// testReducer => state.testReducer
export default function combineReducers(reducers) {
  const reducerKeys = Object.keys(reducers);
  return function combination(state = {}, action) {
    const nextState = {};
    for (let i = 0; i < reducerKeys.length; i++) {
      const key = reducerKeys[i];
      const reducer = reducers[key];
      // 现有状态
      const preStateForKey = state[key];
      const nextStateForKey = reducer(preStateForKey, action);
      nextState[key] = nextStateForKey;
    }
    return nextState;
  };
}
